﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Automation;
using LEET.CommonInterfaces.Automation;

namespace Tedds_Tool.Automatables
{
    public class RegistryAccessor : IRegistryAccessor
    {
        public void RegisterEventHandler(AutomationEvent eventType, AutomationElement element, AutomationEventHandler handler)
        {
            try
            {
                // If this is F**KED UP make sure you are refreshing the process before the
                // AutomationElement is created
                Automation.AddAutomationEventHandler
                (
                    eventType,
                    element,
                    TreeScope.Subtree,
                    handler
                );
            }
            catch (Exception e)
            {
                return ;
            }
        }

        public void RemoveEventHandler(AutomationEvent eventType, AutomationElement element, AutomationEventHandler handler)
        {
            Automation.RemoveAutomationEventHandler(eventType, element, handler);
        }

        public void RegisterEventHandler(AutomationElement element, StructureChangedEventHandler handler)
        {
            try
            {
                // If this is F**KED UP make sure you are refreshing the process before the
                // AutomationElement is created
                Automation.AddStructureChangedEventHandler
                (
                    element,
                    TreeScope.Subtree,
                    handler
                );
            }
            catch (Exception e)
            {
                return;
            }
        }

        public void RemoveEventHandler(AutomationElement element, StructureChangedEventHandler handler)
        {
            Automation.RemoveStructureChangedEventHandler(element, handler);
        }

        public void RegisterEventHandler(AutomationProperty eventType, AutomationElement element, AutomationPropertyChangedEventHandler handler)
        {
            Automation.AddAutomationPropertyChangedEventHandler 
            (
                element,
                TreeScope.Subtree,
                handler,
                eventType
            );
        }

        public void RemoveEventHandler(AutomationProperty eventType, AutomationElement element, AutomationPropertyChangedEventHandler handler)
        {
            Automation.RemoveAutomationPropertyChangedEventHandler(element, handler);
        }

    }
}
